var u3dobject = {
	eventType : {
			PROGRESS : "progress",
			COMPLETE : "complete"
	},
	jsListener : [],	
	flashListener : [],
	unityObjects : [],
	
	addJsListener : function (eventType, callback) 
        {
		if(u3dobject.jsListener[eventType]) 
                {
			u3dobject.jsListener[eventType].push(callback);
		}
		else
		{
			u3dobject.jsListener[eventType] = [callback];
		}
		
		u3dobject.log("Adding Js Listener. EventType => " + eventType);
	},
	
	addFlashListener : function (eventType, flash_id)
	{
		if(u3dobject.flashListener[eventType])
		{
			u3dobject.flashListener[eventType].push(flash_id);
		}
		else
		{
			u3dobject.flashListener[eventType] = [flash_id];
		}
		
		u3dobject.log("Adding Flash Listener. EventType => " + eventType + " FlashId => " + flash_id);
	},        
	
	removeJsListener : function (eventType, callback)
	{
		if(u3dobject.jsListener[eventType]) {
			for(var i = 0; i < u3dobject.jsListener[eventType].length; i++)
			{
				if(u3dobject.jsListener[eventType][i] == callback)
				{
					u3dobject.jsListener[eventType].splice(i,1);
					u3dobject.log("Removing Js Listener. EventType => " + eventType);
					break;
				}
			}
		}		
	},
	
	removeFlashListener : function (eventType, flash_id)
	{
		if(u3dobject.flashListener[eventType])
		{
			for(var i = 0; i < u3dobject.flashListener[eventType].length; i++)
			{
				if(u3dobject.flashListener[eventType][i] == flash_id)
				{
					u3dobject.flashListener[eventType].splice(i,1);
					u3dobject.log("Removing Flash Listener. EventType => " + eventType + " FlashId => " + flash_id);
					break;
				}
			}
		}		
	},
	
	removeAllJsListener : function ( eventType )
	{
		if(u3dobject.jsListener[eventType]) {
			u3dobject.jsListener[eventType] = [];
		}
		
		u3dobject.log("Removing All Js Listener. EventType => " + eventType);
	},
	
	removeAllFlashListener : function ( eventType )
	{
		if(u3dobject.flashListener[eventType]) {
			u3dobject.flashListener[eventType] = [];
		}
		
		u3dobject.log("Removing All Flash Listener. EventType => " + eventType);
	},
	
	progress : function(progress)
	{
		if( u3dobject.jsListener[u3dobject.eventType.PROGRESS] )
		{
			for(var i = 0; i < u3dobject.jsListener[u3dobject.eventType.PROGRESS].length ; i++)
			{
				var callback = u3dobject.jsListener[u3dobject.eventType.PROGRESS][i];
				callback(progress);
			}
		}
		
		if( u3dobject.flashListener[u3dobject.eventType.PROGRESS] )		
		{
			for(var i = 0; i < u3dobject.flashListener[u3dobject.eventType.PROGRESS].length ; i++)
			{
				var callback = u3dobject.flashListener[u3dobject.eventType.PROGRESS][i];
				u3object.callflash(callback, u3dobject.eventType.PROGRESS, progress);			
			}		
		}
		
		//u3dobject.log("Progress => " + progress);
	},
	
	complete : function()
	{		
		if( u3dobject.jsListener[u3dobject.eventType.COMPLETE] )		
		{
			for(var i = 0; i < u3dobject.jsListener[u3dobject.eventType.COMPLETE].length ; i++)
			{
				var callback = u3dobject.jsListener[u3dobject.eventType.COMPLETE][i];
				callback();
			}
		}
		
		if( u3dobject.flashListener[u3dobject.eventType.COMPLETE] )		
		{
			for(var i = 0; i < u3dobject.flashListener[u3dobject.eventType.COMPLETE].length ; i++)
			{
				var callback = u3dobject.flashListener[u3dobject.eventType.COMPLETE][i];
				u3object.callflash(callback, u3dobject.eventType.COMPLETE, progress);			
			}
		}
		
		u3dobject.log("Complete");
	},
	
	callFlash : function(flash_id, function_name, params)
	{
		var el = document.getElementById(flash_id);
		
		if(el.u3dobject)
		{
			 el.u3dobject(function_name, params);
		}
		else
		{
			u3dobject.log("U3dobject was not initialized in flash. Please, call first U3DObject.Active()");
		}
		
		u3dobject.log("CallFlash. FlashId => " + flash_id + " FunctionName => " + function_name + " Params => " + params);
	},
	
	callUnity : function (unity_id, function_name, params)
	{
		var unity = unityObject.getObjectById(unity_id);
		
		unity.SendMessage("U3Object", "CallbackListener", JSON.stringify({function_name : function_name, params : params}));
		
		u3dobject.log("CallUnity. UnityId => " + unity_id + " FunctionName => " + function_name + " Params => " + params);
	},
	
	log : function(msg)
	{
		if(console && console.log)
		{
			console.log(msg);
		}
	},
	/* This a wrapper for UnityObject.js Can return [missing,installed,broken,x64] */
	isUnityInstalledFromFlash : function (flash_id)
	{
		unityObject.detectUnity(function(p)
		{
			if(flash_id)
			{
				u3dobject.callFlash(flash_id, "IsUnityInstalledCallback", p);
			}
		});
	},
	/* Can return [missing,installed,broken,x64] */
	isUnityInstalled : function (callback)
	{
		unityObject.detectUnity(function(p)
		{
			if(callback)
			{
				callback(p);
			}
		});
	},
	
	show : function(div_id)
	{
		var el = unityObject.getObjectById(div_id);
		var div = document.getElementById(div_id);
		
		if(u3dobject.unityObjects[div_id]['width'] != parseInt(u3dobject.unityObjects[div_id]['width']))
		{
			el.style.width = u3dobject.unityObjects[div_id]['width'];
			div.style.width = u3dobject.unityObjects[div_id]['width'];
		}
		else
		{
			el.style.width = u3dobject.unityObjects[div_id]['width'] + "px";
			div.style.width = u3dobject.unityObjects[div_id]['width'] + "px";
		}
		
		if(u3dobject.unityObjects[div_id]['height'] != parseInt(u3dobject.unityObjects[div_id]['height']))
		{
			el.style.height = u3dobject.unityObjects[div_id]['height'];
			div.style.height = u3dobject.unityObjects[div_id]['height'];
		}
		else
		{
			el.style.height = u3dobject.unityObjects[div_id]['height'] + "px";
			div.style.height = u3dobject.unityObjects[div_id]['height'] + "px";
		}
	},
	
	hide : function(div_id)
	{
		var el = unityObject.getObjectById(div_id);
		el.style.width = "1px";
		el.style.height = "1px";
		
		var div = document.getElementById(div_id);
		div.style.width = "1px";
		div.style.height = "1px";
	},
	
	embed : function(div, unityFile, width, height, params, attributes, callback)
	{
		var embedInfo = {
			div : div,
			unityFile : unityFile,
			width : width,
			height : height,
			params : params,
			attributes : attributes,
			callback : callback
		};
		
		u3dobject.unityObjects[div] = embedInfo;
		
		unityObject.embedUnity(div, unityFile, width, height, params, attributes, callback);
	}
};